// server.js

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());

const port = 8000;

function getLocalTime() {
    return new Date().toLocaleString();
}

let user_list = [
    {
        user_id: 1,
        user_info: {
            user_name: 'test1',
            password: '123'
        }
    },
    {
        user_id: 2,
        user_info: {
            user_name: 'test2',
            password: '123'
        }
    }
];

let post_list = [
    {
        post_id: 1,
        post_info: {
            title: 'title 1',
            content: 'content 1',
            author: {
                user_name: 'author name 1',
                user_id: 1,
            },
            update_time: '2022-12-20 10:00:00',
            vote: '1'
        },
        comment_list: [
            {
                comment_id: 1,
                comment_info: {
                    content: 'post 1 comment 1',
                    author: {
                        user_name: 'comment name 1',
                    },
                    update_time: '2022-12-21 1:00:21',
                }
            },
            {
                comment_id: 2,
                comment_info: {
                    content: 'post 1 comment 2',
                    author: {
                        user_name: 'comment name 2',
                    },
                    update_time: '2022-12-21 3:00:21',
                }
            }
        ],
    },
    {
        post_id: 2,
        post_info: {
            title: 'title 2',
            content: 'content 2',
            author: {
                user_name: 'author name 2',
                user_id: 2,
            },
            update_time: '2022-12-20 12:00:00',
            vote: '2'
        },
        comment_list: [
            {
                comment_id: 1,
                comment_info: {
                    content: 'post 2 comment 1',
                    author: {
                        user_name: 'comment name 1',
                    },
                    update_time: '2022-12-28 1:00:21',
                }
            },
            {
                comment_id: 2,
                comment_info: {
                    content: 'post 2 comment 2',
                    author: {
                        user_name: 'comment name 2',
                    },
                    update_time: '2022-12-28 3:00:21',
                }
            }
        ],
    }
];

// get methods
app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.get('/post/getall', (req, res) => {
    res.send(post_list);
});

app.get('/post/get', (req, res) => {
    const { post_id } = req.query;
    console.log(`get /post/get request, post_id = ${post_id}`);
    let response = [];
    for (let i in post_list) {
        if (post_list[i].post_id === parseInt(post_id)) {
            response = post_list[i];
            break;
        }
    }
    res.send(response);
});

app.get('/comment/get', (req, res) => {
    const { post_id } = req.query;
    console.log('get /comment/get request, post_id = ', post_id);
    let response = [];
    for (let i in post_list) {
        if (post_list[i].post_id === parseInt(post_id)) {
            response = post_list[i].comment_list;
            break;
        }
    }
    res.send(response);
});


// post methods
app.post('/user/login', (req, res) => {
    const { user_name, password } = req.body;
    console.log(`get /user/login request, user_name = ${user_name}, password = ${password}`);
    let existing = false;
    let user_id = -1;
    for (let i in user_list) {
        let user_info = user_list[i]['user_info'];
        if (user_info['user_name'] === user_name && user_info['password'] === password) {
            existing = true;
            user_id = user_list[i]['user_id'];
            break;
        }
    }
    res.send({
        user_id: user_id
    });
});

app.post('/post/create', (req, res) => {
    const { title, content, author_name, author_id } = req.body;
    let new_id = post_list.length + 1;
    console.log('get /post/create request, new_id = ', new_id);
    post_list.push({
        post_id: new_id,
        post_info: {
            title: title,
            content: content,
            author: {
                user_name: author_name,
                user_id: author_id,
            },
            update_time: getLocalTime(),
            vote: 0,
            comment_list: [],
        }
    });
    res.send({
        success: true
    });
});

app.listen(port, () => {
    console.log(`express simple server app is listening at http://localhost:${port}`);
});
